/*
 * 通用函数
*/
function Util(){
	this.getUserAgent = navigator.userAgent;
	this.isGecko = this.getUserAgent.indexOf("Gecko") != -1;
	this.isOpera = this.getUserAgent.indexOf("Opera") != -1;
	this.getOffset = function (el, isLeft) {
		var retValue = 0;
		while (el != null) {
			retValue += el["offset" + (isLeft ? "Left" : "Top")];
			el = el.offsetParent;
		}
		return retValue;
	};
	this.bindFunction = function (el, fucName) {
		return function () {
			return el[fucName].apply(el, arguments);
		};
	};
	this.re_calcOff = function (el) {
		for (var i = 0; i < this.dragArray.length; i++) {
			var ele = this.dragArray[i];
			ele.elm.pagePosLeft = this.getOffset(ele.elm, true);
			ele.elm.pagePosTop = this.getOffset(ele.elm, false);
		}
		var nextSib = el.elm.nextSibling;
		while (nextSib) {
			nextSib.pagePosTop -= el.elm.offsetHeight;
			nextSib = nextSib.nextSibling;
		}
	};
	
	this.hide = function () {
		this.rootElement.style.display = "none";
	};
	
	this.show = function () {
		this.rootElement.style.display = "";
	};
	
	this.getElementsByClassName = function(node, className) {
		var children = node.getElementsByTagName('*');
		var elements = new Array();
		for (var i=0; i<children.length; i++) {
			var child = children[i];
			var classNames = child.className.split(' ');
			for (var j = 0; j < classNames.length; j++) {
				if (classNames[j] == className) {
					elements.push(child);
					break;
				}
			}
		}
		return elements;
	}
}

/*
 * 拖动时的克隆层
*/
ghostElement = null;
getGhostElement = function () {
    if (!ghostElement) {
        ghostElement = document.createElement("DIV");
        ghostElement.className = "divdrag_ghost";
        ghostElement.backgroundColor = "";
        ghostElement.style.border = "3px dashed red";
        ghostElement.innerHTML = "&nbsp;";
    }
    return ghostElement;
};

/*
 * 可拖动元素
 *tp:util对象
 *el:拖拉对象
*/
function draggable(el,header,tp) {
	
	this._dragStart = function() {
			this.tool.re_calcOff(this);
			this.origNextSibling = this.elm.nextSibling;
			var _ghostElement = getGhostElement();
			var offH = this.elm.offsetHeight;
			if (this.tool.isGecko) {
				offH -= parseInt(_ghostElement.style.borderTopWidth) * 2;
			}
			var offW = this.elm.offsetWidth;
			var offLeft = this.tool.getOffset(this.elm, true);
			var offTop = this.tool.getOffset(this.elm, false);
			this.tool.hide();
			this.elm.style.width = offW + "px";
			_ghostElement.style.height = offH + "px";
			this.elm.parentNode.insertBefore(_ghostElement, this.elm.nextSibling);
			this.elm.style.position = "absolute";
			this.elm.style.zIndex = 100;
			this.elm.style.left = offLeft + "px";
			this.elm.style.top = offTop + "px";
			this.tool.show();
			this.isDragging = false;
			return false;
		};
	
	this._drag = function(clientX, clientY) {
		if (!this.isDragging) {
			this.elm.style.filter = "alpha(opacity=70)";
			this.elm.style.opacity = 0.7;
			this.isDragging = true;
		}
	
		var found = null;
		var max_distance = 100000000;
		for (var i = 0; i < this.tool.dragArray.length; i++) {
			var ele = this.tool.dragArray[i];
			var distance = Math.sqrt(Math.pow(clientX - ele.elm.pagePosLeft, 2) + Math.pow(clientY - ele.elm.pagePosTop, 2));
			if (ele == this) {
				continue;
			}
			if (isNaN(distance)) {
				continue;
			}
			if (distance < max_distance) {
				max_distance = distance;
				found = ele;
			}
		}
		var _ghostElement = getGhostElement();
		if (found != null && _ghostElement.nextSibling != found.elm) {
			found.elm.parentNode.insertBefore(_ghostElement, found.elm);
			if (this.tool.isOpera) {
				document.body.style.display = "none";
				document.body.style.display = "";
			}
		}
	};
	
	this._dragEnd = function() {
		//alert("_dragEnd");
		if (this._afterDrag()) {
			 saveNodePosition(this.elm)
		}
		return true;
	};
	
	this._afterDrag = function() {
		var returnValue = false;
		this.tool.hide();
		this.elm.style.position = "";
		this.elm.style.width = "";
		this.elm.style.zIndex = "";
		this.elm.style.filter = "";
		this.elm.style.opacity = "";
		var ele = getGhostElement();
		if (ele.nextSibling != this.origNextSibling) {
			ele.parentNode.insertBefore(this.elm, ele.nextSibling);
			returnValue = true;
		}
		ele.parentNode.removeChild(ele);
		this.tool.show();
		if (this.tool.isOpera) {
			document.body.style.display = "none";
			document.body.style.display = "";
		}   
		return returnValue;
	};
	
    this.isDragging = false;
		this.tool = tp;
    this.elm = el;
    this.header = document.getElementById(el.id + header);
    this.hasIFrame = this.elm.getElementsByTagName("IFRAME").length > 0;
    if (this.header) {
        this.header.style.cursor = "move";
        Drag.init(this.header, this.elm);
        this.elm.onDragStart = this.tool.bindFunction(this, "_dragStart");
        this.elm.onDrag = this.tool.bindFunction(this, "_drag");
        this.elm.onDragEnd = this.tool.bindFunction(this, "_dragEnd");
    }
	
};

//保存拖动后节点位置
function saveNodePosition(obj){		
	//当前被拖动节点
	var currentNode = obj;
	//当前容器节点
	var parentNode = obj.parentNode;	
	//alert(parentNode.childNodes.length);		
	//找到当前节点位置并记录
	for(i = 0; i < parentNode.childNodes.length; i++ ){			
		var node = parentNode.childNodes(i);
		//alert(node.id);
		if(node.id == currentNode.id){
			var trueId = node.id;
			trueId = trueId.substr(3,100);
			//alert(trueId);
			var url = "dndAction.do?method=updateDrogSource&targetName="+ parentNode.id +"&orderNum=" + i + "&tempId=" + trueId + "&time" + new Date().getTime();;
			
			updateDragSource(url);
			//alert(url + "当前列数为" + i + ",父节点名称为:" + parentNode.id);
		}
	}

}

/*
 * 实现拖动函数
*/
var Drag = {
	obj:null, 
	init:function (elementHeader, element) {
    	elementHeader.onmousedown = Drag.start;
	    elementHeader.obj = element;
	    if (isNaN(parseInt(element.style.left))) {
	        element.style.left = "0px";
	    }
	    if (isNaN(parseInt(element.style.top))) {
	        element.style.top = "0px";
	    }
	    element.onDragStart = new Function();
	    element.onDragEnd = new Function();
	    element.onDrag = new Function();
	},
	start:function (event) {
	    var element = Drag.obj = this.obj;
	    event = Drag.fixE(event);
	    if (event.which != 1) {
	        return true;
    	}
	    element.onDragStart();
	    element.lastMouseX = event.clientX;
	    element.lastMouseY = event.clientY;
	    document.onmouseup = Drag.end;
	    document.onmousemove = Drag.drag;
	    return false;
	}, 

	drag:function (event) {
	    event = Drag.fixE(event);
	    if (event.which == 0) {
	        return Drag.end();
    	}
	    var element = Drag.obj;
	    var _clientX = event.clientY;
	    var _clientY = event.clientX;
	    if (element.lastMouseX == _clientY && element.lastMouseY == _clientX) {
	        return false;
	    }
	    var _lastX = parseInt(element.style.top);
	    var _lastY = parseInt(element.style.left);
	    var newX, newY;
	    newX = _lastY + _clientY - element.lastMouseX;
	    newY = _lastX + _clientX - element.lastMouseY;
	    element.style.left = newX + "px";
	    element.style.top = newY + "px";
	    element.lastMouseX = _clientY;
	    element.lastMouseY = _clientX;
	    element.onDrag(newX, newY);
	    return false;
	},
	end:function (event) {
			//alert("Drag end");
	    event = Drag.fixE(event);
	    document.onmousemove = null;
	    document.onmouseup = null;
	    var _onDragEndFuc = Drag.obj.onDragEnd();
	    Drag.obj = null;
	    return _onDragEndFuc;
	}, 
	fixE:function (ig_) {
	    if (typeof ig_ == "undefined") {
	        ig_ = window.event;
	    }
	    if (typeof ig_.layerX == "undefined") {
	        ig_.layerX = ig_.offsetX;
	    }
	    if (typeof ig_.layerY == "undefined") {
	        ig_.layerY = ig_.offsetY;
	    }
	    if (typeof ig_.which == "undefined") {
	        ig_.which = ig_.button;
	    }
    	return ig_;
	}
};

/*
 * 初始化拖动
*/
var _Rss_initDrag = function (el) {	
    Util.rootElement = el;
    Util.dragArray = new Array();
    var counter = 0;
    for (var i = 0; i < el.childNodes.length; i++) {
        var ele = el.childNodes[i];		
        for (var j = 0; j < ele.childNodes.length; j++) {
            var ele1 = ele.childNodes[j];		
            if (ele1.tagName == "DIV" && ele1.className=="divdrag_body") {            	
                Util.dragArray[counter] = new draggable(ele1,"_h");
                counter++;
            }
        }
    }
};

var _initDrag = function (el,classname) {
    Util.rootElement = el;
    Util.dragArray = new Array();
    var counter = 0;
    for (var i = 0; i < el.childNodes.length; i++) {
        var ele = el.childNodes[i];		
        for (var j = 0; j < ele.childNodes.length; j++) {
            var ele1 = ele.childNodes[j];		
            if (ele1.tagName == "DIV" && ele1.className.indexOf(classname)>-1) {				
                Util.dragArray[counter] = new draggable(ele1,"_header");
                counter++;
            }
        }
    }
};

var _initDragObj = function (el,classname,header) {	
    var temp = new Util(); 
    temp.rootElement = el;
    temp.dragArray = new Array();
    var counter = 0;
    for (var i = 0; i < el.childNodes.length; i++) {
        var ele = el.childNodes[i];		
        for (var j = 0; j < ele.childNodes.length; j++) {        	
            var ele1 = ele.childNodes[j];		
            if (ele1.tagName == "DIV" && ele1.className.indexOf(classname)>-1) {				
                temp.dragArray[counter] = new draggable(ele1,header,temp);
                counter++;
            }
        }
    }
};